package com.lzh.runke.util;

import java.util.LinkedList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @Author GuoShun
 * @description
 * @Date 2021/11/3 10:47
 * @Version 1.0
 */
public class ListStringUtils {


    // 交集
    public static List<String> intersection(List<String> list1,List<String> list2){
        List<String> intersection = list1.stream().filter(list2::contains).collect(toList());
        //这样写也可以
        //List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
        return intersection;
    }

    // 差集 (list1 - list2)
    public static List<String> subtraction1(List<String> list1,List<String> list2){
        List<String> subtraction = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
        return subtraction;
    }

    // 差集 (list2 - list1)
    public static void subtraction2(List<String> list1,List<String> list2){
        List<String> subtraction = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
        System.out.println("差集:list2 - list1");
        System.out.println(subtraction);
    }

    // 并集
    public static void union(List<String> list1,List<String> list2){
        List<String> listAll1 = list1.parallelStream().collect(toList());
        List<String> listAll2 = list2.parallelStream().collect(toList());
        listAll1.addAll(listAll2);
        System.out.println("list1、list2求并集：");
        System.out.println(listAll1);
    }

    // 去重并集
    public static void deduplicationUnion(List<String> list1,List<String> list2){
        List<String> listAll1 = list1.parallelStream().collect(toList());
        List<String> listAll2 = list2.parallelStream().collect(toList());
        listAll1.addAll(listAll2); //并集
        List<String> listAllDistinct = listAll1.stream().distinct().collect(toList());//去重
        System.out.println("list1、list2求去重并集：");
        System.out.println(listAllDistinct);
    }

    public static void main(String[] args) {
        List<Integer> list = new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list.get(2));
    }
}
